package com.jiyi.activity.datainput.sm;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jiyi.activity.datainput.DIBaseMorePicActivity;
import com.jiyi.bean.SingleImageBean;
import com.jiyi.bean.datainput.new_input.BaseCheck;
import com.jiyi.bean.datainput.new_input.SpaceLawBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.inteface.DownloadPicOverInterface;
import com.jiyi.retrofit.HttpMethods;
import com.jiyi.widget.NoScrollGridView;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.base.AppMediaType;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MultipartBody;

/**
 * Created by Administrator on 2018/3/22.
 */

public class BuildSpaceLawActiviy extends DIBaseMorePicActivity implements DownloadPicOverInterface {
    /* 头部 */
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;


    @BindView(R.id.rbShouXuYes)
    RadioButton rbShouXuYes;
    @BindView(R.id.rbShouXuNo)
    RadioButton rbShouXuNo;
    @BindView(R.id.rgShouXuIsOK)
    RadioGroup rgShouXuIsOK;
    @BindView(R.id.etShouXuProblem)
    EditText etShouXuProblem;
    @BindView(R.id.etShouXuMeasure)
    EditText etShouXuMeasure;
    @BindView(R.id.rbSafeCheckYes)
    RadioButton rbSafeCheckYes;
    @BindView(R.id.rbSafeCheckNo)
    RadioButton rbSafeCheckNo;
    @BindView(R.id.rgSafeCheckIsOK)
    RadioGroup rgSafeCheckIsOK;
    @BindView(R.id.etSafeCheckProblem)
    EditText etSafeCheckProblem;
    @BindView(R.id.etSafeCheckMeasure)
    EditText etSafeCheckMeasure;
    @BindView(R.id.rbUseFuncYes)
    RadioButton rbUseFuncYes;
    @BindView(R.id.rbUseFuncNo)
    RadioButton rbUseFuncNo;
    @BindView(R.id.rgUseFuncIsOK)
    RadioGroup rgUseFuncIsOK;
    @BindView(R.id.etUseFuncProblem)
    EditText etUseFuncProblem;
    @BindView(R.id.etUseFuncMeasure)
    EditText etUseFuncMeasure;


    String build_id = "";
    int mShouXuIsOK = 0;
    int mSafeCheckYes = 0;
    int mUseFuncYes = 0;


    @BindView(R.id.gvShouXu)
    NoScrollGridView gvShouXu;
    @BindView(R.id.gvSafeCheck)
    NoScrollGridView gvSafeCheck;
    @BindView(R.id.gvUseFunc)
    NoScrollGridView gvUseFunc;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_build_space_law);
        ButterKnife.bind(this);
        init();
    }

    public void init() {
        getIntentData();
        setTop();
        setViewStatus();
        getData();
        super.setDownloadPicOverInterface(this);
    }

    private void getIntentData() {
        build_id = getIntent().getStringExtra("build_no");
        if (TextUtils.isEmpty(build_id))
            build_id = "";
    }

    private void setTop() {
        tvTitle.setText("建筑场所消防行政许可");
        tvSearch.setCompoundDrawables(null, null, null, null);
        tvSearch.setText("保存");
    }

    public void setViewStatus() {
        rgShouXuIsOK.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbShouXuYes) {
                    mShouXuIsOK = 1;
                } else {
                    mShouXuIsOK = 0;
                }
            }
        });

        rgSafeCheckIsOK.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbSafeCheckYes) {
                    mSafeCheckYes = 1;
                } else {
                    mSafeCheckYes = 0;
                }
            }
        });

        rgUseFuncIsOK.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbUseFuncYes) {
                    mUseFuncYes = 1;
                } else {
                    mUseFuncYes = 0;
                }
            }
        });
    }


    public void getData() {
        HttpMethods.getInstance().getSpaceLawInfo(UserInfoUtil.getCurrentUserAccessToken(), build_id, 2)
                .subscribe(new SampleProgressObserver<SpaceLawBean>(this) {
                    @Override
                    public void onNext(SpaceLawBean spaceLawBean) {
                        if (spaceLawBean == null || "".equals(spaceLawBean.getGson_null())) {
                            spaceLawBean = new SpaceLawBean();
                            BaseCheck bc1 = new BaseCheck();
                            bc1.setIs_ok(0);
                            bc1.setProblem("");
                            bc1.setMeasures("");
                            spaceLawBean.setHandle_procedures(bc1);
                            BaseCheck bc2 = new BaseCheck();
                            bc2.setIs_ok(0);
                            bc2.setProblem("");
                            bc2.setMeasures("");
                            spaceLawBean.setSecurity_check(bc2);
                            BaseCheck bc3 = new BaseCheck();
                            bc3.setIs_ok(0);
                            bc3.setProblem("");
                            bc3.setMeasures("");
                            spaceLawBean.setChange_use(bc3);
                            spaceLawBean.setNo_edit(1);
                            spaceLawBean.getHandle_procedures().setImage(new ArrayList<String>());
                            spaceLawBean.getSecurity_check().setImage(new ArrayList<String>());
                            spaceLawBean.getChange_use().setImage(new ArrayList<String>());
                        }

                        fillData(spaceLawBean);
                    }
                });
    }

    private void fillData(SpaceLawBean spaceLawBean) {
        if (spaceLawBean.getHandle_procedures().getIs_ok() == 1) {
            rbShouXuYes.setChecked(true);
        }

        etShouXuProblem.setText(spaceLawBean.getHandle_procedures().getProblem());
        etShouXuMeasure.setText(spaceLawBean.getHandle_procedures().getMeasures());

        if (spaceLawBean.getSecurity_check().getIs_ok() == 1) {
            rbSafeCheckYes.setChecked(true);
        }

        etSafeCheckProblem.setText(spaceLawBean.getSecurity_check().getProblem());
        etSafeCheckMeasure.setText(spaceLawBean.getSecurity_check().getMeasures());

        if (spaceLawBean.getChange_use().getIs_ok() == 1) {
            rbUseFuncYes.setChecked(true);
        }

        etUseFuncProblem.setText(spaceLawBean.getChange_use().getProblem());
        etUseFuncMeasure.setText(spaceLawBean.getChange_use().getMeasures());

        if (spaceLawBean.getNo_edit() == 1) {
            tvSearch.setVisibility(View.VISIBLE);
        } else {
            tvSearch.setVisibility(View.INVISIBLE);
        }

        initGridViewList(spaceLawBean);
    }

    private void doPost() {
        Map<String, String> a1 = new HashMap<>();
        a1.put("is_ok", mShouXuIsOK + "");
        a1.put("problem", etShouXuProblem.getText().toString());
        a1.put("measures", etShouXuMeasure.getText().toString());

        Map<String, String> a2 = new HashMap<>();
        a2.put("is_ok", mSafeCheckYes + "");
        a2.put("problem", etSafeCheckProblem.getText().toString());
        a2.put("measures", etSafeCheckMeasure.getText().toString());

        Map<String, String> a3 = new HashMap<>();
        a3.put("is_ok", mUseFuncYes + "");
        a3.put("problem", etUseFuncProblem.getText().toString());
        a3.put("measures", etUseFuncMeasure.getText().toString());

        Map<String, Map<String, String>> dataParams = new HashMap<>();
        dataParams.put("handle_procedures", a1);
        dataParams.put("security_check", a2);
        dataParams.put("change_use", a3);

        MultipartBody.Builder builder = new MultipartBody.Builder();

        putBody(builder, AppConfig.ACCESS_TOKEN, AppMediaType.TEXT, UserInfoUtil.getCurrentUserAccessToken());
        putBody(builder, "build_id", AppMediaType.TEXT, build_id);
        putBody(builder, "type", AppMediaType.TEXT, 1 + "");
        putBody(builder, "data", AppMediaType.TEXT, new Gson().toJson(dataParams));
        putBody(builder, "action_type", AppMediaType.TEXT, "license");


        // 图片数组
        File file;
        int size = mulGridViewBean.getSingleImageList().size();
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < mulGridViewBean.getSingleImageList().get(i).getImageList().size(); j++) {
                if (mulGridViewBean.getSingleImageList().get(i).getImageList().get(j).contains("http")) {
                    Log.e("ttt     ", mulGridViewBean.getSingleImageList().get(i).getImageList().get(j) + "\n");
                    continue;
                }
                else
                {
                    file = new File(mulGridViewBean.getSingleImageList().get(i).getImageList().get(j));
                    if (i ==0)
                    {
                        putBody(builder, "image*handle_procedures[]", AppMediaType.JPG, file);
                    }
                    else if (i ==1)
                    {
                        putBody(builder, "image*security_check[]", AppMediaType.JPG, file);
                    }
                    else if (i ==2)
                    {
                        putBody(builder, "image*change_use[]", AppMediaType.JPG, file);
                    }
                    else
                    {
                        continue;
                    }
                }
            }
        }

        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();

        HttpMethods.getInstance().postSpaceLawInfo(multipartBody)
                .subscribe(new SampleProgressObserver<SpaceLawBean>(this) {
                    @Override
                    public void onNext(SpaceLawBean spaceLawBean) {
                        ToastUtil.getInstances().showMsg("保存成功");
                    }
                });

    }

    void initGridViewList(SpaceLawBean spaceLawBean)
    {
        List<SingleImageBean> singleImageBeans = new ArrayList<SingleImageBean>();

        SingleImageBean s1 = new SingleImageBean();
        s1.setPosition(0);
        s1.setDelModel(false);
        s1.setGridView(gvShouXu);
        s1.setImageList(spaceLawBean.getHandle_procedures().getImage());
        singleImageBeans.add(s1);

        SingleImageBean s2 = new SingleImageBean();
        s2.setPosition(1);
        s2.setDelModel(false);
        s2.setGridView(gvSafeCheck);
        s2.setImageList(spaceLawBean.getSecurity_check().getImage());
        singleImageBeans.add(s2);

        SingleImageBean s3 = new SingleImageBean();
        s3.setPosition(2);
        s3.setDelModel(false);
        s3.setGridView(gvUseFunc);
        s3.setImageList(spaceLawBean.getChange_use().getImage());
        singleImageBeans.add(s3);

        mulGridViewBean.setSingleImageList(singleImageBeans);
        refreshAdapter();
    }

    @OnClick({R.id.tvBack, R.id.tvSearch})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            case R.id.tvSearch:
//                doPost();
                downloadImage();
                break;
            default:
                break;
        }
    }

    @Override
    public void post() {
        doPost();
    }
}
